package cn.leyaber.hrm.web.comtroller;

import cn.leyaber.hrm.redusutils.AliyunOSSUtil;
import cn.leyaber.hrm.util.AjaxResult;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

public class OssAlibbyComtroller {
     @PostMapping("/aliyunOSS/upload")
     @ResponseBody
     public AjaxResult aliyunOSS(@RequestBody MultipartFile file, HttpSession session, HttpServletRequest request){
         try {
             //获取上传文件的名称
             String fileName = file.getOriginalFilename();
             System.out.println(fileName);
             String pathUrl = session.getServletContext().getRealPath("photo")+File.separator+fileName;
             System.out.println(pathUrl);
             String realPath = request.getSession().getServletContext().getRealPath(request.getRequestURI());
             System.out.println(realPath);

             String path = AliyunOSSUtil.upLoad(new File(pathUrl));

             return AjaxResult.me().setResultObj(path).setMessage("上传成功");
         }catch (Exception e){
             return AjaxResult.me().setSuccess(false).setMessage("上传失败");
         }
     }
    }


